package com.kc.sjwh.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kc.sjwh.dao.JqbzzdDao;
import com.kc.sjwh.entity.bo.JqbzzdBo;
import com.kc.sjwh.entity.po.Jqbzzd;
import com.kc.sjwh.entity.vo.dict.DictItem;
import com.kc.sjwh.other.QueryParam;

/**
 * 警情标注字典Service
 * @author zhouy
 * @date 2018年1月16日
 */
@Service
public class JqbzzdService {
	static Logger logger = Logger.getLogger(JqxzService.class);
	
	@Autowired
	JqbzzdDao jqbzzdDao;
	
	/**
	 * 获得树结构
	 * @return
	 */
	public List<DictItem> tree() {
		List<Jqbzzd> list = jqbzzdDao.getList(new QueryParam());
		List<DictItem> diList = new ArrayList<DictItem>();
		for(Jqbzzd one : list){
			DictItem di = new DictItem();
			di.setId(one.getBzbh());
			di.setName(one.getBzmc());
			di.setPid(one.getFbzbh());
			diList.add(di);
			if(isRoot(list, one)) {
				di.setPid("root");
			}
		}
		DictItem root = new DictItem();
		root.setId("root");
		root.setName("资源");
		root.setPid(null);
		diList.add(root);
		return diList;
	}
	
	/**
	 * 获取单个信息
	 * @param bzbh
	 * @return
	 */
	public JqbzzdBo getOne(String bzbh) {
		return (JqbzzdBo) jqbzzdDao.getOne("getOne", new QueryParam().put("bzbh", bzbh));
	}
	
	/**
	 * 删除资源
	 * @param bzbh
	 * @return
	 */
	public void delete(String bzbh) {
		Jqbzzd one = new Jqbzzd();
		one.setBzbh(bzbh);
		one.setSybz("N");
		jqbzzdDao.update(one, "bzbh");
		//递归删除所有下级资源
		List<Jqbzzd> list = jqbzzdDao.getList(new QueryParam().addSqlCondition(" t.fbzbh = '" + bzbh + "'"));
		if(list.size() > 0){
			deleteAll(list);
		}
	}
	
	/**
	 * 删除所有下级资源
	 */
	private void deleteAll(List<Jqbzzd> list){
		for(int i = 0 ; i < list.size() ; i++){
			Jqbzzd one = new Jqbzzd();
			one.setBzbh(list.get(i).getBzbh());
			one.setSybz("N");
			jqbzzdDao.update(one, "bzbh");
			//继续查找是否还有下级
			List<Jqbzzd> sonlist = jqbzzdDao.getList(
					new QueryParam().addSqlCondition(" t.fbzbh = '" + 
							list.get(i).getBzbh() + "'"));
			if(sonlist.size() > 0){
				deleteAll(sonlist);
			}
		}
	}
	
	private boolean isRoot(List<Jqbzzd> list, Jqbzzd zd) {
		boolean find = false;
		for(Jqbzzd one : list) {
			if(one.getBzbh().equals(zd.getFbzbh())) {
				find = true;
			}
		}
		return !find;
	}
	
	/**
	 * 编辑警情标注字典信息
	 */
	public void updateJqbzzdInfo(Jqbzzd one){
		jqbzzdDao.update(one, "bzbh");
	}
	
	public void addInfo(Jqbzzd one){
		QueryParam p = new QueryParam();
		//设置 BZBH
		String fbzbh = one.getFbzbh();
		if(null == fbzbh || "".equals(fbzbh)){ //无上级
			fbzbh = "";
			p.addSqlCondition(" t.fbzbh is null");
		}else{
			p.addSqlCondition(" t.fbzbh = '" + fbzbh + "'");
		}
		JqbzzdBo bo = (JqbzzdBo) jqbzzdDao.getList("getBzbh", p).get(0);
		String bzbh = "";
		if(null == bo){
			bzbh = fbzbh +"01";
		}else{
			bzbh = bo.getBzbh();
			if(null == bzbh || "".equals(bzbh)){
				bzbh = fbzbh +"01";
			}
		}
		one.setBzts("");
		one.setBzbh(bzbh);
		jqbzzdDao.add(one);
	}
}
